package com.hyf.treeview.util;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hyf.treeview.entity.Employee;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/29.
 */

public class AppUtil {

    /**
     * 获取数据
     *
     * @param context
     * @return
     */
    public static final List<Employee> getEmployeeData(Context context) {
        List<Employee> list = new ArrayList<>();
        try {
            InputStream is = context.getAssets().open("TestData.json");
            BufferedReader bf = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = bf.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
            bf.close();
            is.close();

            JSONArray jsonArray = JSON.parseArray(sb.toString());
            if (jsonArray != null) {
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    boolean bm = jsonObject.getBoolean("bm");
                    String id = jsonObject.getString("id");
                    String name = jsonObject.getString("name");
                    String pId = jsonObject.getString("pId");
                    String realname = jsonObject.getString("realname");
                    Employee chuanyuerenTree = new Employee(bm, id, pId, name, realname);
                    list.add(chuanyuerenTree);
                }
            }
        } catch (Exception e) {
        }
        return list;
    }
}
